package cn.tedu.charging.order.rabbit;


import cn.tedu.charging.order.config.RabbitConfiguration;
import cn.tedu.charging.order.pojo.po.OrderMQPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class RabbitMQOrderProducer {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    public void sendOrder(OrderMQPO orderMQPO){
        rabbitTemplate.setMandatory(true);
        rabbitTemplate.setReturnsCallback(returnedMessage -> {
            log.debug("returnMessage:{}",returnedMessage);
        });
        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
            log.debug("RabbitMQ确认回调:correlationData:{},ack:{},cause:{}");
            if(ack){
                log.debug("RabbitMQ成功收到了消息");
            }else{
                log.debug("RabbitMQ没有收到了消息,打日志,进行重试,重新发送");
            }
        });
        rabbitTemplate.convertAndSend(RabbitConfiguration.EXCHANGE_NAME,
                RabbitConfiguration.ROUTING_KEY,orderMQPO);
    }
}
